<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Test select.add() method');

debug('Test select');
test(false);

debug('Test select multiple');
test(true);

var select;

function resetSelect(multiple) {
    select = document.createElement('select');
    select.multiple = multiple;
    select.appendChild(new Option("0", "0", false, false));
    select.appendChild(new Option("1", "1", false, false));
    select.appendChild(new Option("2", "2", false, false));
}

function addOption(index) {
    select.add(new Option('X', 'X', false, false), index);
    return getSelectOptions();
}

function getSelectOptions() {
    var values = [];
    for (var i = 0; i < select.options.length; ++i) {
        values.push(select.options[i].value);
    }
    return values.join(",");
}

function test(multiple) {
    resetSelect(multiple);
    shouldBeEqualToString('addOption(0)', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(1)', '0,X,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(2)', '0,1,X,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(3)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(100)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(-100)', '0,1,2,X');

    resetSelect(multiple);
    select.add(new Option('X', 'X', false, false));
    shouldBeEqualToString('getSelectOptions()', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(undefined)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(null)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(NaN)', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(Infinity)', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(-Infinity)', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption("foo")', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption("0")', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption("1")', '0,X,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(false)', 'X,0,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(true)', '0,X,1,2');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(2147483647)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(-2147483647)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(2147483648)', '0,1,2,X');

    resetSelect(multiple);
    shouldBeEqualToString('addOption(-2147483648)', '0,1,2,X');

    resetSelect(multiple);
    shouldThrow('select.add()');
    shouldThrow('select.add("foo")');
    shouldThrow('select.add("foo", 0)');
    shouldThrow('select.add(undefined)');
    shouldThrow('select.add(undefined, 0)');
    shouldThrow('select.add(null)');
    shouldThrow('select.add(null, 0)');
    shouldBeUndefined('select.add(new Option("X", "X", false, false), 0, 1)');
    shouldBeUndefined('select.add(new Option("X", "X", false, false), select.options[0], 2)');

    resetSelect(multiple);
    var group = document.createElement('optgroup');
    group.appendChild(new Option('X', 'X', false, false));
    group.appendChild(new Option('Y', 'Y', false, false));
    select.add(group, 1);
    shouldBeEqualToString('getSelectOptions()', '0,X,Y,1,2');
}
</script>
</body>
</html>
